
package com.feib.soeasy.action;

import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.security.web.util.RequestMatcher;

import com.feib.soeasy.model.Group;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.security.SoeasyDelegatingAuthenticationEntryPoint;
import com.feib.soeasy.service.GroupService;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.ValidationErrorHandler;
import net.sourceforge.stripes.validation.ValidationErrors;

/**
 * @author  Anson Tsai
 * @version 1.0, 2010/11/28
 * @see	    
 * @since 
 */
public class LoginActionBean extends BaseActionBean implements ValidationErrorHandler
{
    private static final String DEFAULT_JSP = "/login.jsp";
    
    /**
     * 業者代號查詢畫面
     */
    private static final String AJAX_QUERY_GROUP_JSP = "/WEB-INF/views/login/groupPopUpList.jsp";
    

    /**
     * 帳單業者查詢條件
     */
    private Group qryGroup;
    
    /**
     * 帳單業者查詢結果
     */
    private PaginatedList<Group> queryGroupResult;
    
    @SpringBean("groupService")
    private GroupService groupService;
    
    /* eBIlling功能提升_第二階段, eBilling分由不同頁面登入 */
    @SpringBean("soeasyAuthenticationEntryPoint")
    private SoeasyDelegatingAuthenticationEntryPoint soeasyAuthenticationEntryPoint;

    @DontBind
    @DefaultHandler
    public Resolution doDefault()
    {
        logger.debug("Enter doDefault mehtod.");
        
        LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints = soeasyAuthenticationEntryPoint.getEntryPoints();
        
        HttpServletRequest request = this.getContext().getRequest();
        for (RequestMatcher requestMatcher : entryPoints.keySet()) {
			if (requestMatcher.matches(request))
			{
				LoginUrlAuthenticationEntryPoint ep = (LoginUrlAuthenticationEntryPoint)entryPoints.get(requestMatcher);
				//.commence(request, response, authException);
				return new ForwardResolution(ep.getLoginFormUrl());
			}
		}
        return new ForwardResolution(((LoginUrlAuthenticationEntryPoint)soeasyAuthenticationEntryPoint.getDefaultEntryPoint()).getLoginFormUrl()); 
    }
    
    /**
     * 帳單業者查詢
     * @return
     */
    public Resolution doAjaxQueryGroups()
    {
        logger.debug("Enter doAjaxQueryGroups mehtod.");
        try {
            //USER未輸入查詢條件，所以查詢物件會是NULL(查詢物件不可為NULL)
            if (null == qryGroup)
                qryGroup = new Group();
            
            //呼叫服務查詢
            if (null == queryGroupResult)
                queryGroupResult = new PaginatedList<Group>();
            queryGroupResult = groupService.queryByGroupExcludeBank(qryGroup, queryGroupResult);
        }
        catch (Exception e) {
            logger.error("系統登入-帳單業者查詢錯誤(LoginActionBean.doAjaxQueryGroups):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQuery.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }

        return new ForwardResolution(AJAX_QUERY_GROUP_JSP);
    }

    
    /**
     * @return the qryGroup
     */
    public Group getQryGroup() {
        return qryGroup;
    }

    
    /**
     * @param qryGroup the qryGroup to set
     */
    public void setQryGroup(Group qryGroup) {
        this.qryGroup = qryGroup;
    }

    
    /**
     * @return the queryGroupResult
     */
    public PaginatedList<Group> getQueryGroupResult() {
        return queryGroupResult;
    }

    
    /**
     * @param queryGroupResult the queryGroupResult to set
     */
    public void setQueryGroupResult(PaginatedList<Group> queryGroupResult) {
        this.queryGroupResult = queryGroupResult;
    }
}
